import java.util.*;

/**
 * LinkedHashMap insertion order test
 */
public class LinkedHashMapTest {
    
    public static void main(String[] args) {
        System.out.println("=== Testing LinkedHashMap insertion order ===");
        
        // Test 1: Simple insertion order
        testSimpleInsertionOrder();
        
        // Test 2: Replacement order
        testReplacementOrder();
        
        // Test 3: Multiple runs consistency
        testMultipleRunsConsistency();
    }
    
    private static void testSimpleInsertionOrder() {
        System.out.println("\n--- Test 1: Simple insertion order ---");
        
        Map<String, String> map = new LinkedHashMap<>();
        map.put("key3", "value3");
        map.put("key1", "value1");
        map.put("key2", "value2");
        
        System.out.println("Insertion order: key3, key1, key2");
        System.out.println("KeySet order: " + new ArrayList<>(map.keySet()));
    }
    
    private static void testReplacementOrder() {
        System.out.println("\n--- Test 2: Replacement order ---");
        
        Map<String, String> map = new LinkedHashMap<>();
        map.put("key1", "value1");
        map.put("key2", "value2");
        map.put("key3", "value3");
        
        System.out.println("Initial order: " + new ArrayList<>(map.keySet()));
        
        // Replace key2
        map.put("key2", "new_value2");
        System.out.println("After replacing key2: " + new ArrayList<>(map.keySet()));
        
        // Replace key1
        map.put("key1", "new_value1");
        System.out.println("After replacing key1: " + new ArrayList<>(map.keySet()));
    }
    
    private static void testMultipleRunsConsistency() {
        System.out.println("\n--- Test 3: Multiple runs consistency ---");
        
        for (int run = 1; run <= 3; run++) {
            Map<String, String> map = new LinkedHashMap<>();
            
            // Simulate the deduplication logic
            String[] items = {"item1", "item2", "item3", "item1", "item4", "item2"};
            
            for (String item : items) {
                if (map.containsKey(item)) {
                    // Replace existing
                    map.put(item, "updated_" + item);
                } else {
                    // Add new
                    map.put(item, item);
                }
            }
            
            System.out.println("Run " + run + " result: " + new ArrayList<>(map.keySet()));
        }
    }
}
